#include <iostream>

bool isPrime(int num) {
    if (num < 2) return false;
    for (int i = 2; i*i <= num; i++) {
        if (num % i == 0) return false;
    }
    return true;
}

int main() {
    int n;
    std::cin >> n;

    if (n < 1000 || n > 9999) {
        std::cout << 0;
        return 0;
    }

    int maxPrime = 0;
    for (int i = 2; i <= n; i++) {
        if (isPrime(i)) {
            std::string str = std::to_string(n);
            std::string str_i = std::to_string(i);
            int pos = 0;
            for (int j = 0; j < str_i.length(); j++) {
                pos = str.find(str_i[j], pos);
                if (pos == std::string::npos) {
                    pos = -1;
                    break;
                }
                pos++;
            }
            if (pos != -1) {
                maxPrime = std::max(maxPrime, i);
            }
        }
    }

    std::cout << maxPrime;

    return 0;
}